package com.icarcom.cn.util;

import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 * Created by Administrator on 2020/6/2.
 *
 * jl
 */

public class DesCode {
    public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
    public static final String DES = "DES";
    public static String charset = "utf-8";
    public static final String CAR_LP_DES_END_DEC_KEY = "kjijijjhijj12365";
    //public static final String CAR_LP_DES_END_DEC_KEY = "cUs9G0i6Ab9YL9X0";


    /***
     *  DES加密
     * @param res
     * @return
     */
    public static String DESencode(String res) {
        return keyGeneratorES(res, true);
    }

    public static String DESdecode(String res) {
        return keyGeneratorES(res, false);
    }

    /***
     *
     * @param isEncode
     * @return
     */
    private static String keyGeneratorES(String res, boolean isEncode) {
        try {
            DESKeySpec desKeySpec = new DESKeySpec(CAR_LP_DES_END_DEC_KEY.getBytes());
            SecretKeyFactory factory = SecretKeyFactory.getInstance(DES);
            SecretKey convertedKey = factory.generateSecret(desKeySpec);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            if (isEncode) {
                cipher.init(Cipher.ENCRYPT_MODE, convertedKey);
                byte[] result = cipher.doFinal(res.getBytes());
                return parseByte2HexStr(result);
            } else {
                cipher.init(Cipher.DECRYPT_MODE, convertedKey);
                byte[] result = cipher.doFinal(parseHexStr2Byte(res));
                return new String(result, charset);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /***
     *
     */
    public static String parseByte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < buf.length; i++) {
            String hex = Integer.toHexString(buf[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /***
     *
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }


    /**
     * 32位MD5加密的大写字符串
     *
     * @param s
     * @return
     */
    public final static String MD5(String s) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F' };
        try {
            byte[] btInput = s.getBytes();
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            mdInst.update(btInput);
            byte[] md = mdInst.digest();
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
